﻿// --------------------------------------------------------------------------------------------------------------------
// (c) Copyright Microsoft Corporation.
// This source is subject to the Microsoft Public License (Ms-PL).
// Please see http://go.microsoft.com/fwlink/?LinkID=131993 for details.
// All other rights reserved.
// http://blogs.msdn.com/b/devdave/archive/2010/10/04/a-better-slider-for-windows-phone-7.aspx
// --------------------------------------------------------------------------------------------------------------------

namespace WP7Contrib.View.Controls.Sliders
{
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Media;


    public class PhoneSlider : Slider
    {
        public PhoneSlider()
        {
            SizeChanged += this.PhoneSliderSizeChanged;
        }

        void PhoneSliderSizeChanged(object sender, SizeChangedEventArgs e)
        {
            if (e.NewSize.Width > 0 && e.NewSize.Height > 0)
            {
                Rect clipRect = new Rect(0, 0, e.NewSize.Width, e.NewSize.Height);
                if (Orientation == System.Windows.Controls.Orientation.Horizontal)
                {
                    clipRect.X -= 12;
                    clipRect.Width += 24;
                    var margin = Resources["PhoneHorizontalMargin"];
                    if (margin != null)
                    {
                        Margin = (Thickness)margin;
                    }
                }
                else
                {
                    clipRect.Y -= 12;
                    clipRect.Height += 24;
                    var margin = Resources["PhoneVerticalMargin"];
                    if (margin != null)
                    {
                        Margin = (Thickness)margin;
                    }
                }

                this.Clip = new RectangleGeometry() { Rect = clipRect };
            }
        }
    }
}